﻿using SAIM.Controllers;
using SAIM.Models;
using SAIM.Models.BLL;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace SAIM
{
    public class MvcApplication : System.Web.HttpApplication
    {
        private SAIMIdentity UserIdentity
        {
            get { return new SAIMIdentity(User.Identity); }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AntiForgeryConfig.UniqueClaimTypeIdentifier = System.IdentityModel.Claims.ClaimTypes.Upn;
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            if (Context.IsCustomErrorEnabled)
                ShowCustomErrorPage(Server.GetLastError());
        }

        private void ShowCustomErrorPage(Exception exception)
        {
            HttpException httpException = exception as HttpException;
            if (httpException == null)
                httpException = new HttpException(500, "Internal Server Error", exception);

            Response.Clear();
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Exception");
            routeData.Values.Add("appErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
                case 403:
                    routeData.Values.Add("action", "AccessDenied");
                    break;
                case 404:
                    routeData.Values.Add("action", "NotFound");
                    break;
                case 500:
                    routeData.Values.Add("action", "ServerError");
                    break;
                default:
                    routeData.Values.Add("action", "UnhandledError");
                    routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                    break;
            }
            Server.ClearError();
            // Ensure content-type header is present
            Response.Headers.Add("Content-Type", "text/html");
            IController controller = new ExceptionController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string Id = User != null ? UserIdentity.GetUserId() : String.Empty;
            string lang;

            try
            {
                using (AspNetUsersBLL user = new AspNetUsersBLL(Id))
                {
                    lang = user.GetUserLanguage();
                }
            }
            catch (Exception) { 
                lang = "es-mx";
            }
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        }
    }
}
